package 其他;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;

/**
 * InputStreamReader与FileReader主要区别
 * <p>
 * FileReader继承InputStreamReader类
 * InputStreamReader可以通过构造方法指定字符编码，和缓充内存大小
 */
public class Stream {

    public static void main(String[] args) throws Exception {

        testCreate();

    }

    public static void testCreate() throws Exception {

        test();
        File file = new File("D:\\b.csv");
        System.out.println(file.exists());

    }

    public static void test() throws Exception {
        InputStream is = new FileInputStream("D:\\a\\310430154990038.csv");
        FileOutputStream os = new FileOutputStream("D:\\b.csv");
        byte[] buffer = new byte[1024];
        boolean var10 = false;

        int i;
        while ((i = is.read(buffer)) != -1) {
            os.write(buffer, 0, i);
        }

        System.out.println("下载成功");
        os.close();
        is.close();
        return;
    }

    public static void bufferStream() throws Exception {
        String content = "";
        File file1 = new File("D:\\a\\a.txt");
        //输入流不会自动创建文件，也不会创建目录
        if (!file1.getParentFile().exists()) {
            //只能创建一层目录
            file1.getParentFile().mkdir();
        }
        if (!file1.exists()) {
            file1.createNewFile();
        }
        //输出流自动创建文件，不能创建目录
        File file2 = new File("D:\\b.txt");
        try (InputStream inputStream = new FileInputStream(file1);
             //是否追加输出，默认覆盖
             OutputStream outputStream = new FileOutputStream(file2, true);
             BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
             BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream))) {
            while ((content = bufferedReader.readLine()) != null) {
                bufferedWriter.write(content);
                //换行
                bufferedWriter.newLine();
            }
        } catch (Exception e) {
            System.out.println(e);
        }
    }

    public static void zipStream() throws Exception {
        String content = "";
        String filePath = "D:\\a.zip";
        File file1 = new File(filePath);
        ZipFile zipFile = new ZipFile(filePath);
        File file2 = new File("D:\\b.txt");

        ZipEntry zipEntry;
        try (InputStream inputStream = new FileInputStream(file1);
             OutputStream outputStream = new FileOutputStream(file2, true);
             InputStream bufferedInputStream = new BufferedInputStream((inputStream));
             ZipInputStream zipInputStream = new ZipInputStream(bufferedInputStream);
             BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream))
        ) {
            while ((zipEntry = zipInputStream.getNextEntry()) != null) {
                if (!zipEntry.isDirectory()) {
                    try (LineNumberReader reader = new LineNumberReader(new InputStreamReader(zipFile.getInputStream(zipEntry)));
                         BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(zipFile.getInputStream(zipEntry), StandardCharsets.UTF_8), 5 * 1024 * 1024)) {
                        //获取文件总行数
                        reader.skip(Long.MAX_VALUE);
                        //lineCount有多少回车，实际行数是lineCount+1
                        int lineCount = reader.getLineNumber();
                        System.out.println("实际行数:" + (lineCount + 1));
                        while ((content = bufferedReader.readLine()) != null) {
                            bufferedWriter.write(content);
                            bufferedWriter.newLine();
                        }
                    }
                }
            }
        } catch (Exception e) {
            System.out.println(e);
        }
    }


}
